/**
 * 
 */
package com.seguriapp.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.seguriapp.R;
import com.seguriapp.adapters.TramitesAdapter;
import com.seguriapp.controllers.TramitesController;
import com.seguriapp.dao.TramitesDao;
import com.seguriapp.entities.TramiteEntity;

/**
 * @author pjohnson
 * 
 */
public class TramitesActivity extends Activity {

	private ListView lista;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentView(R.layout.activity_tramites_list);
		llenarLista();
		lista.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final TramiteEntity tramite = (TramiteEntity) lista
						.getItemAtPosition(position);
				final Intent intent = new Intent();
				intent.setClass(mContext, TramiteDetalleActivity.class);

				intent.putExtra("name", tramite.getName());
				intent.putExtra("tramiteId", tramite.getId());
				intent.putExtra("id", tramite.gettId());
				intent.putExtra("hasFile", tramite.isHasFile());
				intent.putExtra("tId", tramite.gettId());

				if (tramite.getCost() != null) {
					intent.putExtra("cost", tramite.getCost());
				}
				if (tramite.getDuration() != null) {
					intent.putExtra("duration", tramite.getDuration());
				}

				startActivity(intent);
				overridePendingTransition(R.anim.aparecer_desde_derecha,
						R.anim.desaparecer_fading);
			}

		});
	}

	public void llenarLista() {
		final TramitesController tController = new TramitesController(this);
		final Cursor cursorTramites = tController.getTramites();
		final String[] arrayFrom = new String[] { TramitesDao.COL_NAME };
		final int[] arrayTo = new int[] { R.id.entity_title };
		lista = (ListView) findViewById(R.id.tramites_list);
		final TramitesAdapter adapter = new TramitesAdapter(this,
				R.layout.list_tramites_item, cursorTramites, arrayFrom,
				arrayTo, TramitesAdapter.NO_SELECTION);
		lista.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		final MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.tramites_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.update_menu:
			final TramitesController tController = new TramitesController(
					mContext);
			tController.deleteTramites();
			tController.insertInitialTramites();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
